package com.xuecheng.content.controller;

import com.xuecheng.api.content.TeachplanApi;
import com.xuecheng.api.content.teachplan.dto.TeachplanDTO;
import com.xuecheng.api.content.teachplan.vo.TeachplanVO;
import com.xuecheng.api.media.dto.TeachplanlMediaDTO;
import com.xuecheng.api.media.vo.BindTeachplanMediaVO;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.content.convert.TeachplanConvert;
import com.xuecheng.content.service.TeachplanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程计划 前端控制器
 * </p>
 *
 * @author xuan
 */
@Slf4j
@RestController
//@RequestMapping("teachplanDTO")
public class TeachplanController implements TeachplanApi {

    @Autowired
    private TeachplanService  teachplanService;

    //树形查询
    @GetMapping("teachplan/{courseId}/tree-nodes")
    public TeachplanDTO queryTreeNodesByCourseId(@PathVariable Long courseId) {
        Long companyId = SecurityUtil.getCompanyId();
        //2. 调用service获得 dto数据
        TeachplanDTO teachplanDTO = teachplanService.queryTreeNodesByCourseId(courseId, companyId);
        return teachplanDTO;
    }

    //增加和修改
    @PostMapping("teachplan")
    public TeachplanDTO createOrModifyTeachPlan(@RequestBody TeachplanVO teachplanVO) {
         //1. 获得公司Id
        Long companyId = SecurityUtil.getCompanyId();
        //2. 将vo数据转为 dto 数据
        TeachplanDTO teachplanDTO = TeachplanConvert.INSTANCE.vo2dto(teachplanVO);
        //2.调用service获得 dto数据
        TeachplanDTO result = teachplanService.createOrModifyTeachPlan(teachplanDTO, companyId);
        return result;
    }

    //根据id删除信息
    @DeleteMapping("teachplan/{teachPlanId}")
    public void deleteById( @PathVariable Long teachPlanId) {

    }

    @PostMapping("teachplan/media/association")
    public TeachplanlMediaDTO associateMedia(@RequestBody BindTeachplanMediaVO vo) {
        Long companyId = SecurityUtil.getCompanyId();
        TeachplanlMediaDTO teachplanlMediaDTO = new TeachplanlMediaDTO();

        teachplanlMediaDTO.setTeachplanId(vo.getTeachplanId());
        teachplanlMediaDTO.setMediaId(vo.getMediaId());

        TeachplanlMediaDTO result = teachplanService.associateMedia(teachplanlMediaDTO, companyId);

        return result;

    }

}
